package com.devtools.project.tool.swagger.controller;

import java.util.Arrays;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.devtools.project.tool.swagger.service.WordService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.devtools.framework.aspectj.lang.annotation.Log;
import com.devtools.framework.aspectj.lang.enums.BusinessType;
import com.devtools.project.tool.swagger.domain.ToolSwagger;
import com.devtools.project.tool.swagger.service.ISwaggerService;
import com.devtools.framework.web.controller.BaseController;
import com.devtools.framework.web.page.TableDataInfo;
import com.devtools.framework.web.domain.AjaxResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.devtools.common.utils.text.Convert;
import org.springframework.stereotype.Controller;
import com.devtools.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletResponse;

/**
 * swagger接口信息操作处理
 * 
 * @author devtools
 * @date 2022-11-28
 */
@Controller
@RequestMapping("/tool/swagger")
public class SwaggerController extends BaseController
{
    private String prefix = "tool/swagger";
	@Autowired
	private WordService wordService;
	@Autowired
	private ISwaggerService swaggerService;
	@RequiresPermissions("tool:swagger:view")
	@GetMapping("list.html")
	public String swagger()
	{
	    return prefix + "/swagger";
	}

	@RequiresPermissions("tool:swagger:view")
	@GetMapping()
	public String index()
	{
		return redirect("/swagger-ui.html");
	}
	/**
	 * 查询swagger接口列表
	 */
	@RequiresPermissions("tool:swagger:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(ToolSwagger swagger)
	{
		startPage();
        QueryWrapper<ToolSwagger> queryWrapper = new QueryWrapper<ToolSwagger>(swagger);
		List<ToolSwagger> list = swaggerService.list(queryWrapper);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出swagger接口列表
	 */
	@RequiresPermissions("tool:swagger:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ToolSwagger swagger)
    {
    	QueryWrapper<ToolSwagger> queryWrapper = new QueryWrapper<ToolSwagger>(swagger);
		List<ToolSwagger> list = swaggerService.list(queryWrapper);
        ExcelUtil<ToolSwagger> util = new ExcelUtil<ToolSwagger>(ToolSwagger.class);
        return util.exportExcel(list, "swagger");
    }
	
	/**
	 * 新增swagger接口
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存swagger接口
	 */
	@RequiresPermissions("tool:swagger:add")
	@Log(title = "swagger接口", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(ToolSwagger swagger)
	{		
		return toAjax(swaggerService.save(swagger) ? 1 : 0);
	}

	/**
	 * 修改swagger接口
	 */
	@GetMapping("/edit/{swaggerId}")
	public String edit(@PathVariable("swaggerId") Integer swaggerId, ModelMap mmap)
	{
		ToolSwagger swagger = swaggerService.getById(swaggerId);
		mmap.put("swagger", swagger);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存swagger接口
	 */
	@RequiresPermissions("tool:swagger:edit")
	@Log(title = "swagger接口", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(ToolSwagger swagger)
	{	
		return toAjax(swaggerService.updateById(swagger)? 1 : 0);
	}
	
	/**
	 * 删除swagger接口
	 */
	@RequiresPermissions("tool:swagger:remove")
	@Log(title = "swagger接口", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{	
		List<Integer> lst = Arrays.asList(Convert.toIntArray(ids));	
		return toAjax(swaggerService.removeByIds(lst)? 1 : 0);
	}

	/**
	 * 将 swagger 文档转换成 html 文档，可通过在网页上右键另存为 xxx.doc 的方式转换为 word 文档
	 *
	 * @param model
	 * @return
	 */
	@Deprecated
	@RequestMapping(value = "{swaggerId}/doc", method = {RequestMethod.GET})
	public String getWord(Model model,@PathVariable(value = "swaggerId", required = false) Integer swaggerId) {
		JSONObject data=swaggerService.generateModelData(swaggerId);
		model.addAllAttributes(data);
		return "tool/swagger/word";
	}

	/**
	 * 将 swagger 文档一键下载为 doc 文档
	 */
	@RequestMapping(value = "/downloadWord", method = {RequestMethod.GET})
	public void word(Model model, Integer swaggerId, HttpServletResponse response) {
		JSONObject data=swaggerService.generateModelData(swaggerId);
		model.addAllAttributes(data);
		model.addAttribute("download",0);
		wordService.writeContentToResponse(model,prefix+"/word",data.getString("docName"), response);
	}
}
